package org.alex.samples.service;

import java.util.ArrayList;
import java.util.List;
import org.alex.samples.dao.ISubjectDao;
import org.alex.samples.model.ExamScore;
import org.alex.samples.model.ExamScoreVO;
import org.alex.samples.model.Subject;
import org.alex.samples.model.SubjectVO;
import org.alex.samples.model.UserSubject;
import org.alex.samples.model.UserSubjectVO;
import org.alex.samples.service.ISubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ISubjectServiceImpl implements ISubjectService {

	@Autowired
	ISubjectDao subjectDao;

	public List<SubjectVO> findAllSubject() throws Exception {
	  List<SubjectVO> subjectVOs=new ArrayList<SubjectVO>();
	  List<Subject> subjects=subjectDao.findAllSubject();
	  for(int  i= 0;i< subjects.size();i++){
		  subjectVOs.add(new SubjectVO());
		  BeanUtils.copyProperties(subjects.get(i), subjectVOs.get(i));
		  
		  List<UserSubjectVO> userSubjects=new ArrayList<UserSubjectVO>();
		  int j=0;
		  for(UserSubject userSubject:subjects.get(i).getUserSubject()){
			  userSubjects.add(new UserSubjectVO());
			  BeanUtils.copyProperties(userSubject, userSubjects.get(j++));
		  }
		  subjectVOs.get(i).setUserSubjectVOs(userSubjects);
		  
		  List<ExamScoreVO> examScores=new ArrayList<ExamScoreVO>();
		  j=0;
		  for(ExamScore examScore:subjects.get(i).getExamScore()){
			  examScores.add(new ExamScoreVO());
			  BeanUtils.copyProperties(examScore, examScores.get(j++));
		  }
		  subjectVOs.get(i).setExamScoreVOs(examScores);
	  }
	   
      return subjectVOs;
   }

	public SubjectVO findSubjectById(Integer id) throws Exception {
		Subject subject = subjectDao.findSubjectById(id);
		SubjectVO subjectVO=new SubjectVO();
		BeanUtils.copyProperties(subject, subjectVO);
		
		List<UserSubjectVO> userSubjects=new ArrayList<UserSubjectVO>();
		int i=0;
		for(UserSubject userSubject:subject.getUserSubject()){
			userSubjects.add(new UserSubjectVO());
			BeanUtils.copyProperties(userSubject, userSubjects.get(i++));
		}
		subjectVO.setUserSubjectVOs(userSubjects);
		
		List<ExamScoreVO> examScores=new ArrayList<ExamScoreVO>();
		for(ExamScore score:subject.getExamScore()){
			examScores.add(new ExamScoreVO());
			BeanUtils.copyProperties(score, examScores.get(i++));
		}
		subjectVO.setExamScoreVOs(examScores);
		
		return subjectVO;
	}

	public void EditSubject(Subject subject) throws Exception {
		this.subjectDao.EditSubject(subject);
	}

	public void createSubject(Subject subject) throws Exception {
		this.subjectDao.createSubject(subject);
	}

	public void deleteSubject(Integer id) throws Exception {
		this.subjectDao.deleteSubject(id);
	}

	@Override
	public List<String> getAllUserBySubject(Integer subjectId) throws Exception {
		return subjectDao.getAllUserBySubject(subjectId);
	}

	@Override
	public void saveSubjectStudent(List<String> list, Integer subjectId) throws Exception {
		List<String> stuList = subjectDao.getAllUserBySubject(subjectId);
		List<String> inserList = new ArrayList<String>();
		List<String> delList = new ArrayList<String>();
		// 判断哪些是新勾选的学生
		for (String selectedUser : list) {
			if (!stuList.contains(selectedUser)) {
				inserList.add(selectedUser);
			}
		}
		// 判断哪些是原来勾选的学生，现在去掉勾选的
		for (String selectedUser : stuList) {
			if (!list.contains(selectedUser)) {
				delList.add(selectedUser);
			}
		}
		subjectDao.inserSubjectStudent(inserList, subjectId);
		subjectDao.delSubjectStudent(delList, subjectId);
	}

	@Override
	public void changeStatusById(Integer id, Integer status) throws Exception {
		this.subjectDao.changeStatusById(id, status);

	}
}